/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Modele.Contact;
import Modele.TableModel;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.AbstractAction;
import javax.swing.JTable;

/**
 *This class is calle when we click on supprimer button
 * @author Bakalian
 */
public class RemoveAction extends AbstractAction{
        private JTable table;
        private TableModel modele;
        private ArrayList<Contact> list;
    public RemoveAction(JTable table,ArrayList<Contact> c) {
       super("Supprimer");
       this.table=table;
       this.modele=new TableModel(c);
        list=c;
        }
 
        public void actionPerformed(ActionEvent e) {
            
            int[] selection = table.getSelectedRows();
            int[] modelIndexes = new int[selection.length];
 
        for(int i = 0; i < selection.length; i++){
            modelIndexes[i] = table.getRowSorter().convertRowIndexToModel(selection[i]);
            System.out.println(modelIndexes[i]);
        }
 
        Arrays.sort(modelIndexes);
            
            for(int i = selection.length - 1; i >= 0; i--){
                     modele.removeContact(selection[i]);
                     list.remove(i);
                     
            
            }
          
         MenuOperation.DeleteContact(list);
            
            
        }
    
}
